﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewAI.Game;
using NewAI.Tools;

namespace NewAI.FSM.JsonAnalyzerFSM
{
    class BuildActionJsonState : State<JsonAnalyzer>
    {
        private ActionObj actionObj;

        public BuildActionJsonState(ActionObj actionObj)
        {
            this.actionObj = actionObj;
        }

        public override void Enter(JsonAnalyzer entity)
        {
            lock ("同步")
            {
                for (int i = 0; i < entity.actionObjs.Count; i++)
                {
                    if (entity.actionObjs[i] != null && entity.actionObjs[i].PlayerId == actionObj.PlayerId)
                    {
                        entity.actionObjs[i] = actionObj;
                        return;
                    }
                }

                entity.actionObjs.Add(actionObj);
                actionObj = null;

                if (GV.tankCount == entity.actionObjs.Count)
                {
                    entity.CloseTimeOutTimer();
                    PubliceMessage.ActionBulid.Call(null, null);
                    ActionData actionData = new ActionData(entity.roundID, entity.actionObjs);
                    entity.ActionMessage.Call(null, new FSMEventEventArg(actionData.GetActionDataString()));
                    entity.actionObjs.Clear();
                    //entity.timeThread.Abort();
                }
            }
        }
    }
}
